<?php
/**
 * Eren Framework
 * 
 * Define DB class for MySQL
 * 
 * @category System
 * @author Manu Soros Leu
 * @copyright manusoros@gmail.com
 */

class Eren_DB_MySQL extends Eren_DB_Abstract
{
    
    /**
     * Constuct of this class
     * 
     * @param array $config  The config for DB connection
     * @throws Eren_DB_Exception
     */
    public function __construct($config)
    {
        Eren_Validator::checkConfig($config, array('host', 'user', 'password', 'defaultDB', 'port'));
        $this->_DBConfig = $config;
    }
    
    /**
     * Init DB using config
     * 
     * @throws Eren_DB_Exception
     * @return bool
     */
    private function _connect()
    {
        if(!$this->_DB){
            $this->_DB = new mysqli($this->_DBConfig['host'],
                                    $this->_DBConfig['user'],
                                    $this->_DBConfig['password'],
                                    $this->_DBConfig['defaultDB'], 
                                    $this->_DBConfig['port']);
            if($this->_DB->connect_errno != 0){
                throw new Eren_DB_Exception($this->_DB->connect_error, $this->_DB->connect_errno);
            }
        }
        return TRUE;
    }
    
    /**
     * Execute SQL
     * 
     * @param string $SQL
     * @throws Eren_DB_Exception
     * @return object;
     */
    private function _exec($SQL)
    {
        $this->_connect();
        $this->_DB->query("set names utf8;");
        $result = @$this->_DB->query($SQL);
        if($this->_DB->errno !=0 ){
            throw new Eren_DB_Exception($this->_DB->error, $this->_DB->errno);
        }
        return $result;
    }
    
    /**
     * (non-PHPdoc)
     * @see Eren_DB_Abstract::DB()
     */
    public function DB($DBName)
    {
        $this->_connect();
        if($this->_DBConfig['defaultDB'] != $DBName){
            $this->_DBConfig['defaultDB'] = $DBName;
            $this->_DB->select_db($DBName);
            if($this->_DB->errno !=0 ){
                throw new Eren_DB_Exception($this->_DB->error, $this->_DB->errno);
            }
        }
        return TRUE;
    }
    
    /**
     * (non-PHPdoc)
     * @see Eren_DB_Abstract::select()
     */
    public function select($SQL)
    {
        $resultObj = $this->_exec($SQL);
        $result['count'] = $resultObj->num_rows;
        $result['data']  = array();
        while($row = $resultObj->fetch_assoc()){
            $result['data'][] = $row;
        }
        return $result;
    }
    
    /**
     * (non-PHPdoc)
     * @see Eren_DB_Abstract::insert()
     */
    public function insert($SQL)
    {
        $this->_exec($SQL);
        return $this->_DB->insert_id;
    }
    
    /**
     * (non-PHPdoc)
     * @see Eren_DB_Abstract::update()
     */
    public function update($SQL)
    {
        $this->_exec($SQL);
        return TRUE;
    }
    
    /**
     * (non-PHPdoc)
     * @see Eren_DB_Abstract::delete()
     */
    public function delete($SQL)
    {
        $this->_exec($SQL);
        return TRUE;
    }
    
    /**
     * (non-PHPdoc)
     * @see Eren_DB_Abstract::count()
     */
    public function count($SQL)
    {
        $data = $this->select($SQL);
        return current($data['data'][0]);
    }
}
